package com.baizhi.controller;

import com.baizhi.entity.*;
import com.baizhi.service.BookService;
import com.baizhi.service.ItemService;
import com.baizhi.service.OrderService;
import com.baizhi.util.JedisUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("back/order")
public class OrderController {
    private ObjectMapper objectMapper=new ObjectMapper();

    @Autowired
    private BookService bookService;
    @Autowired
    private  OrderService orderService;
    @Autowired
    private ItemService itemService;
    @RequestMapping("show")
    public String show(Model model, @RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){
        PageInfo<Order> orderPageInfo = orderService.showOrderPage(pageNum, pageSize);
        model.addAttribute("orders",orderPageInfo);
        return "forward:/back/order/show.jsp";
    }
    @RequestMapping("showItems")
    public String showItems(Model model,Integer orderId){
        Order order = orderService.selectById(orderId);
        model.addAttribute("order",order);
        return  "forward:/back/order/detail.jsp";
    }
    @RequestMapping("addOrder")
    public String addOrder(HttpSession session, Address address) throws JsonProcessingException {
        Jedis jedis = JedisUtils.getJedis();
        String cart1 = jedis.get("cart");
        Cart cart = objectMapper.readValue(cart1, Cart.class);
        User user = (User) session.getAttribute("user");
        Order order=new Order(null, cart.getTotalPrice(), "未支付",address.getName(),address.getMobile(),address.getProvince(),address.getCity(),address.getDistrict(),address.getTown(),address.getAddress(), user.getUserId(), new Date(),null);
        orderService.addOrder(order);
        Integer orderId=order.getId();
        Map<Integer, CartItem> items = cart.getItems();
        for(CartItem c:items.values()){
            itemService.addItem(new Item(null,c.getBook().getBookId(),c.getCount(),new Date(),orderId,null));
        }
        session.setAttribute("cart",cart);
        return  "redirect:/back/order/showOrder.do?id="+orderId+"";
    }
    @RequestMapping("showOrder")
    public String showOrder(Integer id,Model model){
        Order order = orderService.showOrderById(id);
        model.addAttribute("order",order);
        return "forward:/front/pay/pay_order.jsp";
    }
    @RequestMapping("updateOrder")
    public String updateOrder(HttpSession session,Integer id){
        Order order = orderService.showOrderById(id);
        System.out.println("order = " + order);
        order.setStatus("已支付");
        orderService.updateOrder(order);
        Cart cart =(Cart) session.getAttribute("cart");
        Map<Integer, CartItem> items = cart.getItems();
        for(Integer key:items.keySet()){
            CartItem cartItem = items.get(key);
            Book book = cartItem.getBook();
            book.setStock(book.getStock()-cartItem.getCount());
            book.setSale(cartItem.getCount());
            bookService.updateBook(book);
        }
        items.clear();
        return "redirect:/front/main.do";
    }
}
